package com.udima.reglocation;

import java.util.List;





import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.*;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.udima.reglocation.entities.Track;
import com.udima.reglocation.util.SQLiteDatabaseHandler;

public class ListTrackActivity extends android.support.v4.app.FragmentActivity {

    private Button btnList;
    private EditText inputTracks;
//    private TextView registerErrorMsg;    
    private SQLiteDatabaseHandler db;
    
    // Google Map
    private GoogleMap googleMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_track);      
        db = new SQLiteDatabaseHandler(this);
        
        try {
            // Loading map
            initilizeMap();
        } catch (Exception e) {
            e.printStackTrace();
        }

        inputTracks = (EditText) findViewById(R.id.txtEmail);
        btnList = (Button) findViewById(R.id.btnList);

//        registerErrorMsg = (TextView) findViewById(R.id.register_error);

        btnList = (Button) findViewById(R.id.btnList);
        
        btnList.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View view) {
            	Editable aux;
            	
            	List <Track> tracks = db.getAllTracks();
            	for(Track tr : tracks) {
            		aux = inputTracks.getText();
            		inputTracks.setText("");
            		inputTracks.setText(aux  
            				 + tr.getLatitude().toString() + '-'
            				 + tr.getLongitude().toString() + '-'
            				 + tr.getAltitude().toString() + '-'
            				 + tr.getSpeed().toString() + '-'
            				 + tr.getTime()
            				 +'\n');
            		 
            		// create marker
            		MarkerOptions marker = new MarkerOptions().position(new LatLng(tr.getLatitude(), tr.getLongitude())).title("Hello Maps ");
            		 
            		// adding marker
            		googleMap.addMarker(marker);
            	}
            }
        });
        
    }
    
    /**
     * function to load map. If map is not created it will create it for you
     * */
	private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
 
            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
            }
        }
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }
 
}